package top.lixunda.ecommerce.server.good.api.entity.vo;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import top.lixunda.ecommerce.server.good.api.entity.db.GoodProduceDO;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 查询商品介绍内容VO
 *
 * @author lixunda
 * @version time:2020/3/11 13:59
 */
@Slf4j
@ApiModel("查询商品介绍内容VO")
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
public class GoodProduceVO {

    @ApiModelProperty(value = "商品介绍编号")
    private Integer id;

    @ApiModelProperty(value = "店铺id")
    private Integer storeId;

    @ApiModelProperty(value = "商品id")
    private Integer goodId;

    @ApiModelProperty(value = "类型，文字描述，图片描述，富文本表示")
    private String contentType;

    @ApiModelProperty(value = "文字描述")
    private String content;

    @ApiModelProperty(value = "本内容连接的商品地址")
    private Integer linkGoodId;

    @ApiModelProperty(value = "本内容连接的子商品编号")
    private Integer linkSubGoodId;

    @ApiModelProperty(value = "本内容连接的地址，站内地址或站外地址")
    private String linkUrl;

    @ApiModelProperty(value = "排序字段，默认为零，可以通过更改此字段改变排序，该字段相同时以创建更新时间排序")
    private Integer sortNumber;

    @ApiModelProperty(value = "新增时间")
    private LocalDateTime createTime;

    @ApiModelProperty(value = "新增人员id")
    private Integer createUserId;

    @ApiModelProperty(value = "新增人员名称")
    private String createUserName;

    @ApiModelProperty(value = "更新时间")
    private LocalDateTime updateTime;

    @ApiModelProperty(value = "更新人员id")
    private Integer updateUserId;

    @ApiModelProperty(value = "更新人员名称")
    private String updateUserName;

    @ApiModelProperty(value = "备注")
    private String remark;

    /**
     * DO转VO
     *
     * @param goodProduceDO GoodProduceDO
     * @return GoodProduceVO
     */
    public static GoodProduceVO fromGoodProduceDO(GoodProduceDO goodProduceDO) {
        if (goodProduceDO == null) {
            return null;
        }
        GoodProduceVO goodProduceVO = new GoodProduceVO();
        BeanUtils.copyProperties(goodProduceDO, goodProduceVO);
        switch (goodProduceDO.getContentType()) {
            case GoodProduceDO.CONTENT_TYPE_TEXT:
                goodProduceVO.setContent(goodProduceDO.getContent());
                break;
            case GoodProduceDO.CONTENT_TYPE_IMAGE:
                goodProduceVO.setContent(goodProduceDO.getImage());
                break;
            case GoodProduceDO.CONTENT_TYPE_RICH_TEXT:
                goodProduceVO.setContent(goodProduceDO.getRichTextContent());
                break;
            default:
                log.error("错误的GoodProduce contentType {}", goodProduceDO);
        }
        return goodProduceVO;
    }

    /**
     * DO转VO，批量转换
     *
     * @param list GoodProduceDO数据列表
     * @return GoodProduceVO数据列表
     */
    public static List<GoodProduceVO> fromGoodProduceDO(List<GoodProduceDO> list) {
        if (list == null) {
            return null;
        }
        return list.stream()
                .map(GoodProduceVO::fromGoodProduceDO)
                .collect(Collectors.toList());
    }
}
